﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.ComponentModel;
using Microsoft.SharePoint.WebPartPages;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint.Search.Query;
using Microsoft.SharePoint;
using System.Xml;
using PS.WebParts.Rollup.Core;
using System.Web.Configuration;

using System.Text;
using Microsoft.Office.Server.Search.Administration;
using PS.WebParts.Rollup.Search.Core.Query;
using PS.WebParts.Rollup.Search.Core;
using System.Xml.Serialization;
using System.Web.UI.WebControls;

namespace PS.WebParts.Rollup.Search
{
    public class SearchDataSource : RollUpDataSource
    {
        public SearchDataSource()
            : base()
        {
            this.Lists = "Search";
            this.SearchInstruction = new SearchInstruction();
            //this.Fields;
        }
        
        protected override ATabularDataSourceView CreateViewInstance()
        {
            SearchDataView dataView = new SearchDataView(HttpContext.Current, this, "defaultView");

            return dataView;            
        }

        protected override System.Web.UI.DataSourceView GetView(string viewName)
        {
            DataSourceView dsv = base.GetView(viewName);
            return dsv;
        }        

        Literal instruction = null;
        public Literal Instruction
        {
            get { return instruction; }
            set
            {
                instruction = value;

                string content = 
                    string.Format("<{1} xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">{0}</{1}>",
                    instruction.Text, SearchInstruction.GetType().Name);

                searchinstruction = (SearchInstruction)SearchInstruction.Desearilize(typeof(SearchInstruction), content);
            }

        }

        private SearchInstruction searchinstruction = null;

        [XmlIgnore]
        public SearchInstruction SearchInstruction
        {
            get { return searchinstruction; }
            set
            {
                if (instruction == null) instruction = new Literal();

                if (searchinstruction == null)
                {
                    searchinstruction = new SearchInstruction();                    
                }

                instruction.Text = searchinstruction.Serialize();
            }
        }
    }
}